package com.yyhh.helpcall.activity;

import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yyhh.helpcall.R;
import com.yyhh.helpcall.http.OkHttpUtils;
import com.yyhh.helpcall.http.bean.BaseResponse;
import com.yyhh.helpcall.http.bean.CustomResponse;
import com.yyhh.helpcall.utils.PhoneUtils;
import com.yyhh.helpcall.utils.ToastUtils;
import java.util.HashMap;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class CustomerDescActivity extends BaseActivity {

    private LinearLayout contentContainer;
    private FrameLayout emptyContainer;
    private TextView phone,name,sex,house,car,money,remark,leve;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_desc);
        initViews();
        initDatas();
    }

    private void initViews(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        phone = findViewById(R.id.phone);
        name = findViewById(R.id.name);
        sex = findViewById(R.id.sex);
        house = findViewById(R.id.has_house);
        car = findViewById(R.id.has_car);
        money = findViewById(R.id.loan_money);
        remark = findViewById(R.id.remark);
        leve = findViewById(R.id.leve);
        contentContainer = findViewById(R.id.content);
        emptyContainer = findViewById(R.id.empty);

//        phone.setText("手机号: ");
//        name.setText("姓名: " );
//        sex.setText("性别: " );
//        leve.setText("等级: " );
//        remark.setText("跟进情况:");
//        house.setText("有无房: " );
//        car.setText("有无车: ");
//        money.setText("需求金额: " );
    }

    private void initDatas(){
        String number = getIntent().getStringExtra("phone");
        showProgressDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",number);
        OkHttpUtils.getInstance().getApi().getCustomerDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<CustomResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<CustomResponse> value) {
                        hiddenProgressDialog();
                        if(value.isSuccess()){
                            CustomResponse data = value.data;
                            phone.setText("手机号: " + PhoneUtils.formatPhone(data.phone));
                            name.setText("姓名: " + data.name);
                            sex.setText("性别: " + (data.sex == 1 ? "男" : data.sex == 2 ? "女" : "未知"));
                            leve.setText("等级: " + data.level + "星");
                            remark.setText("跟进情况:" + data.last_comment);
                            house.setText("有无房: " + (data.has_house == 1 ? "有房" : "无房"));
                            car.setText("有无车: " + (data.has_car == 1 ? "有车" : "无车"));
                            money.setText("需求金额: " + data.loan_amount + "元");
                        }else{
                            emptyContainer.setVisibility(View.VISIBLE);
                            contentContainer.setVisibility(View.GONE);
                            value.showErrorMessage();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        hiddenProgressDialog();
                        e.printStackTrace();
                        ToastUtils.show("出现错误,请重试~");
                    }

                    @Override
                    public void onComplete() {
                        hiddenProgressDialog();
                    }
                });
    }
}
